package activity.discuss;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.laoxinwen.app.R;

import java.util.ArrayList;

import bean.Topic;
import common.BaseActivity;
import common.Constants;
import common.UserConfig;
import common.VideoWebChromeClient;

/**
 * 网络热议
 * Created by sshong on 15/8/19.
 */
public class DiscussActivity extends BaseActivity implements VideoWebChromeClient.VideoFullScreenHandler {

    @id(R.id.topbar)
    private View topBar;

    @id(R.id.tab_container)
    private ViewGroup tabContiner;

    @id(R.id.viewPager)
    private ViewPager viewPager;

    private Topic topic;
    private ArrayList<FrgWebDiscuss> frags;
    private int selectTab = 0;

    private static final String INTENT_KEY_TOPIC = "topic";
    public static final int DISCUSS_TAB_WEIBO = 1;
    public static final int DISCUSS_TAB_ZHIHU = 2;
    public static final int DISCUSS_TAB_TIANYA = 3;

    public static Intent getIntent(Context context, Topic topic) {
        Intent it = new Intent(context, DiscussActivity.class);
        it.putExtra(INTENT_KEY_TOPIC, topic);
        return it;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discuss);
        loadView();
        //title
        TextView titleTxt = f(R.id.tv_title);
        topic = (Topic) getIntent().getSerializableExtra(INTENT_KEY_TOPIC);
        titleTxt.setText("网络热议：" + topic.getTitle());
        //frags
        frags = new ArrayList<>();
        final FrgWebDiscuss wbfrg = new FrgWebDiscuss();
        wbfrg.setData(topic, DISCUSS_TAB_WEIBO);
        frags.add(wbfrg);

        final FrgWebDiscuss zhfrg = new FrgWebDiscuss();
        zhfrg.setData(topic, DISCUSS_TAB_ZHIHU);
        frags.add(zhfrg);

        final FrgWebDiscuss tyfrg = new FrgWebDiscuss();
        tyfrg.setData(topic, DISCUSS_TAB_TIANYA);
        frags.add(tyfrg);
        //viewpager
        setViewPager();
        tabContiner.getChildAt(0).setSelected(true);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_back:
                finish();
                break;
            case R.id.tab_weibo:
                viewPager.setCurrentItem(0);
                break;
            case R.id.tab_zhihu:
                viewPager.setCurrentItem(1);
                break;
            case R.id.tab_tianya:
                viewPager.setCurrentItem(2);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        FrgWebDiscuss frg = frags.get(selectTab);
        if (frg.webChromeClient.inCustomView()) {
            frg.webChromeClient.onHideCustomView();
        } else if (frg.webView.canGoBack()) {
            frg.webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onUpSlide() {
        super.onUpSlide();
        tabContiner.setVisibility(View.GONE);
    }

    @Override
    protected void onDownSlide() {
        super.onDownSlide();
        tabContiner.setVisibility(View.VISIBLE);
    }

    @Override
    public void onFullScreen() {
        topBar.setVisibility(View.GONE);
        tabContiner.setVisibility(View.GONE);
    }

    @Override
    public void onExitFullScreen() {
        topBar.setVisibility(View.VISIBLE);
        tabContiner.setVisibility(View.VISIBLE);
    }

    private void setViewPager() {
        FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return frags.size();
            }

            @Override
            public Fragment getItem(int index) {
                return frags.get(index);
            }
        };
        viewPager.setOffscreenPageLimit(2);
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrollStateChanged(int state) {
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < frags.size(); i++) {
                    tabContiner.getChildAt(i).setSelected(false);
                }
                View selectedChild = tabContiner.getChildAt(position);
                selectedChild.setSelected(true);
                selectTab = position;
            }
        });
    }

}
